package org.mspring.platform.denglu.service;

import java.io.IOException;
import java.util.List;
import javax.annotation.PostConstruct;
import org.hibernate.criterion.Restrictions;
import org.mspring.platform.dao.Order;
import org.mspring.platform.dao.hibernate.common.CriteriaQuery;
import org.mspring.platform.denglu.dao.DengluCommentSyncLogDao;
import org.mspring.platform.denglu.entity.DengluCommentSyncLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2014-05-27 13:38:19
 */
@Service
@Transactional
public class DengluCommentSyncLogService {

    private static final Logger log = LoggerFactory.getLogger(DengluCommentSyncLogService.class);

    private DengluCommentSyncLogDao dengluCommentSyncLogDao;

    @PostConstruct
    public void initTable() {
        try {
            dengluCommentSyncLogDao.initTable();
        } catch (IOException ex) {
            log.error("初始化表失败", ex);
        }
    }

    public void insert(DengluCommentSyncLog log) {
        dengluCommentSyncLogDao.insert(log);
    }

    /**
     * 获取最后一条成功记录
     *
     * @return
     */
    public DengluCommentSyncLog findLastedSuccess() {
        CriteriaQuery query = new CriteriaQuery(DengluCommentSyncLog.class);
        query.addOrder(Order.desc("id"));
        query.setFirstResult(0);
        query.setMaxResults(1);
        query.add(Restrictions.eq("success", true));
        List<DengluCommentSyncLog> list = dengluCommentSyncLogDao.list(query);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Autowired
    public void setDengluCommentSyncLogDao(DengluCommentSyncLogDao dengluCommentSyncLogDao) {
        this.dengluCommentSyncLogDao = dengluCommentSyncLogDao;
    }

}
